﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SQLExpressDAL;


public partial class Manageweb_FanLi_AddUpdateRule : System.Web.UI.Page
{
    string recordID;

    protected void Page_Load(object sender, EventArgs e)
    {
        recordID = Request["RecordID"];

        if (!Page.IsPostBack)
        {
            if (!String.IsNullOrEmpty(recordID))
            {
                this.btnUpdate.Visible = true;
                this.btnAdd.Visible = false;

                BindRule();
            }

        }

    }

    private void BindRule()
    {
        try
        {
            DealRecord obj = new DealRecord();

            DataTable tab = obj.GetSingleRuleByRecordID(recordID);
            DataRow dr = tab.Rows[0];

            this.txtXX.Text = dr["minValue"].ToString();
            this.txtSX.Text = dr["MaxValue"].ToString();
            this.txtDescription.Text = dr["description"].ToString();
            this.txtTJR1.Text = dr["Field1"].ToString();
            this.txtTJR2.Text = dr["Field2"].ToString();
            this.txtTJR3.Text = dr["Field3"].ToString();
            this.txtTJR4.Text = dr["Field4"].ToString();
            this.txtTJR5.Text = dr["Field5"].ToString();
            this.txtTJR6.Text = dr["Field6"].ToString();
            this.txtTJR7.Text = dr["Field7"].ToString();
            this.txtTJR8.Text = dr["Field8"].ToString();

        }
        catch
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('没有相关记录');window.location.href='ManageFanLiRule.aspx'", true);
            //Response.Redirect("~/Manageweb/FanLi/ManageFanLiRule.aspx");
        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Hashtable htab = new Hashtable();
        string msg = "添加成功!";
        try
        {
            double minValue = Convert.ToDouble(this.txtXX.Text.Trim());
            double maxValue = Convert.ToDouble(this.txtSX.Text.Trim());

            string txtTJR = this.txtTJR1.Text;
            float tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field1", tjr);

            txtTJR = this.txtTJR2.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field2", tjr);

            txtTJR = this.txtTJR3.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field3", tjr);

            txtTJR = this.txtTJR4.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field4", tjr);

            txtTJR = this.txtTJR5.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field5", tjr);

            txtTJR = this.txtTJR6.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field6", tjr);

            txtTJR = this.txtTJR7.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field7", tjr);

            txtTJR = this.txtTJR8.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field8", tjr);

            DealRecord obj = new DealRecord();

            int flag = obj.AddNewFanLiRule(minValue, maxValue, htab);

            if (flag == 0)
            {
                msg = "添加失败!";
            }

        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("索引"))
            {
                msg = "添加失败!\\n已存在相同金额范围的规则,请删除后再添加.";
            }
            else
            {
                msg = "添加失败!";
            }
        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", string.Format("alert('{0}')", msg), true);



    }

    protected void UpdateRule()
    {
        Hashtable htab = new Hashtable();
        string msg = "修改成功!";
        try
        {
            double minValue = Convert.ToDouble(this.txtXX.Text.Trim());
            double maxValue = Convert.ToDouble(this.txtSX.Text.Trim());

            string txtTJR = this.txtTJR1.Text;
            float tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field1", tjr);

            txtTJR = this.txtTJR2.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field2", tjr);

            txtTJR = this.txtTJR3.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field3", tjr);

            txtTJR = this.txtTJR4.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field4", tjr);

            txtTJR = this.txtTJR5.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field5", tjr);

            txtTJR = this.txtTJR6.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field6", tjr);

            txtTJR = this.txtTJR7.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field7", tjr);

            txtTJR = this.txtTJR8.Text;
            tjr = string.IsNullOrEmpty(txtTJR) ? 0 : Convert.ToSingle(txtTJR);
            htab.Add("@Field8", tjr);

            DealRecord obj = new DealRecord();

            int flag = obj.UpdateFanLiRule(Convert.ToInt16(recordID), minValue, maxValue,this.txtDescription.Text.Trim(), htab);

            if (flag == 0)
            {
                msg = "修改失败!";
            }

        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("索引"))
            {
                msg = "修改失败!\\n已存在相同金额范围的规则,请删除后再添加.";
            }
            else
            {
                msg = "修改失败!";
            }

        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", string.Format("alert('{0}');window.location.href='ManageFanLiRule.aspx'", msg), true);
        //Response.Redirect("~/Manageweb/FanLi/ManageFanLiRule.aspx");
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        UpdateRule();
    }
}
